﻿

using IotContract.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using XT.Common.Extensions;
using XT.Common.Models.Server;

namespace IotClientService.Extensions;
public static class FmsRemoteExtension
{
    /// <summary>
    /// 提交Fms数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="httpClient"></param>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static async Task<AdminCodeResult<T>> PostFmsData<T>(this HttpClient httpClient, string url, object data)
    {
        try
        {
            httpClient.Timeout = TimeSpan.FromSeconds(10);
            HttpResponseMessage result = await httpClient.PostAsJsonAsync(url, data);
            if (result.IsSuccessStatusCode)
            {
                var str = await result.Content.ReadAsStringAsync();
                var fms= str.ToObject<RemoteReturnModel<T>>();

                return new AdminCodeResult<T>
                {
                    Code = fms.Code,
                    Message = fms.Code + ":" + fms.Msg,
                    Result = fms.Data,
                    Success = fms.Flag
                };
            }

            return new AdminCodeResult<T>
            {
                Code = (int)result.StatusCode,
                Message="500",
                Success=false
            };
        }
        
        catch (Exception ex2)
        {
            Exception ex = ex2;
            return new AdminCodeResult<T>
            {
                Code = 500,
                Success = false,
                Message = "500" + ex.Message
            };
        }
    }


    /// <summary>
    /// 提交Fms数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="httpClient"></param>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static async Task<AdminCodeResult<T>> GetFmsData<T>(this HttpClient httpClient, string url, object data)
    {
        try
        {
            httpClient.Timeout = TimeSpan.FromSeconds(10);
            string query = string.Empty;
            if (data != null)
            {
                query ="?"+ data.ToQueryString();
            }
            HttpResponseMessage result = await httpClient.GetAsync(url+query);
            if (result.IsSuccessStatusCode)
            {
                var str = await result.Content.ReadAsStringAsync();
                var fms = str.ToObject<RemoteReturnModel<T>>();

                return new AdminCodeResult<T>
                {
                    Code = fms.Code,
                    Message = fms.Msg,
                    Result = fms.Data,
                    Success = fms.Flag
                };
            }

            return new AdminCodeResult<T>
            {
                Code = (int)result.StatusCode,
                Success = false
            };
        }
        
        catch (Exception ex2)
        {
            Exception ex = ex2;
            return new AdminCodeResult<T>
            {
                Code = 500,
                Success = false,
                Message = ex.Message
            };
        }
    }
}



